/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.wxs.controller;

import cn.webrx.wxs.mapper.AdminMapper;
import cn.webrx.wxs.utils.Md5SaltUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: wxshop - AdminPasswordController
 * <p>Powered By webrx
 * <p>Created By IntelliJ IDEA On 2021-04-19 17:16:55
 * <p>Description :
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@RestController
public class AdminPasswordController {
    @Autowired
    private AdminMapper admindao;

    @PostMapping("/admin/password/")
    public Map<String, Object> index(String old_password, String password, String re_password, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        var account = session.getAttribute("admin").toString();
        var ma = admindao.checkLogin(account);
        if (Md5SaltUtil.verfiyPassword(account, old_password, ma.get("salt"), ma.get("password"))) {
            //将password 生成密码文和salt 并更新到数据库中

            //加密
            String[] result = Md5SaltUtil.encrytionPassword(account, password);
            Map<String, String> v = new HashMap<>();
            v.put("password", result[0]);
            v.put("salt", result[1]);
            v.put("account", account);
            if (admindao.updatePassword(v) == 1) {
                map.put("code", 1);
                map.put("msg", "密码修改成功，请牢记你的密码。");
            } else {
                map.put("code", 0);
                map.put("msg", "密码修失败，请检查数据是不是正确。");
            }
        } else {
            map.put("code", 0);
            map.put("msg", "修改失败，原密码不正确。");
        }
        return map;
    }

}
